// 云函数入口文件
const cloud = require('wx-server-sdk');
const got = require('got');
cloud.init();

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();

  let res = await got('https://xihuitest.kai12.cn/public/grade_course_student', {
    method: 'POST', //post请求
    headers: {
      'Content-Type': 'application/json'
    }
  });

  if (event.Content == '1' || event.Content == '购买') {
    await cloud.openapi.customerServiceMessage.send({
      touser: wxContext.OPENID,
      msgtype: 'link',
      link: {
        title: '课程名称',
        description: '课程描述' + wxContext.OPENID,
        url: 'http://xhzx.kai12.cn/login_weixin/h5'
      }
    });
  } else {
    await cloud.openapi.customerServiceMessage.send({
      touser: wxContext.OPENID,
      msgtype: 'text',
      text: {
        content: '您好,很高兴为您服务。回复1:购买课程' + JSON.stringify(res.body)
      }
    });
  }

  return 'success';
};
